package com.jeremyaburns.oddio.tracker 
{
	import com.jeremyaburns.oddio.TrackNote;
	import flash.display.Sprite;

	internal class OTNote extends Sprite
	{
		private var mTimeIndex :int;
		private var mPitch     :int;
		private var mDuration  :int;
		
		
		public function OTNote( timeIndex:int, pitch:int, duration:int=1 ) 
		{
			mTimeIndex = timeIndex;
			mPitch     = pitch;
			mDuration  = duration;
			
			graphics.lineStyle( 2, 0xCCCCCC );
			graphics.beginFill( 0xAAAAAA );
			graphics.drawCircle( 0, 0, OTStyle.noteRadius );
			
			changePosition();
		}

		
		public function get timeIndex() :int { return mTimeIndex; }
		public function set timeIndex( rhs:int ) :void
		{
			if( rhs < 0 ) return;
			mTimeIndex = rhs;
			changePosition();
		}
		
		public function get pitch() :int { return mPitch; }
		public function set pitch( rhs:int ) :void
		{
			if( rhs < 0 || rhs > 95 ) return;
			mPitch = rhs;
			changePosition();
		}
		
		public function get duration() :int { return mDuration; }
		public function set duration( rhs:int ) :void
		{
			if( rhs < 1 ) rhs = 1;
			mDuration = rhs;
			changeDuration();
		}
		
		
		private function changePosition() :void
		{
			this.x = Number(OTStyle.noteWidth)/2  + mTimeIndex*OTStyle.noteWidth;
			this.y = Number(OTStyle.noteHeight)/2 + (95-mPitch)*OTStyle.noteHeight;
		}
		
		private function changeDuration() :void
		{
			graphics.clear();
			
			if( duration > 1 ) {
				graphics.lineStyle( OTStyle.noteRadius, 0xCCCCCC );
				graphics.moveTo( 0, 0 );
				graphics.lineTo( OTStyle.noteWidth*(mDuration-1) , 0 );
			}
			
			graphics.lineStyle( 2, 0xCCCCCC );
			graphics.beginFill( 0xAAAAAA );
			graphics.drawCircle( 0, 0, OTStyle.noteRadius );
		}
		
		
		public function getTrackNote() :TrackNote
		{
			return new TrackNote( mTimeIndex, mPitch, mDuration );
		}
	}
}